package swd.basic.conf_prg_info;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
   QueryBean gqbnDAO=null;
   public String action(javax.servlet.jsp.PageContext pageContext){
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
      HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

      View view=(View)request.getAttribute("view");
      (new WebFunction()).setPortal_to_session(request); //set session from portal
      if (request.getSession().getAttribute("u_id")==null) {
         try{
            request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
            return "login";
         }
         catch(Exception le){
            view.setOnloadScript("can not redirect to login page...please contact IT");
            return "";
         }
      }
      //String lstrUserNO = request.getSession().getAttribute("u_id").toString();
      gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

      DataView DataView=null;
      DataViewForm DataViewForm=null;

      DataView = new DataView(view.getDBAlias(),view.getDBDebug(), view.getSourceCharset(), view.getTargetCharset());
      DataViewForm = new DataViewForm();

      request.setAttribute("DataView",DataView);//讓前端可以呼叫dataview
      request.setAttribute("DataViewForm",DataViewForm);

      Business business=new Business(gqbnDAO);
      //Flow flow=new Flow(gqbnDAO,lstrUserNO,"AFS",view,business);

      //String lstrResult=null;
      //String strResult="";
      String strPrgNo="";

      try{
         if(view.getprg_no()!=null && view.getprg_no().trim().length()>0){
            view.setSelectShow("");
            view.setInsertShow("none");
            strPrgNo=view.getprg_no().trim();
            view.setprg_no_readonly(true);
         } else {
            view.setSelectShow("none");
            view.setInsertShow("none");
            view.setprg_no_readonly(false);

         }

         business.setDataViewForm(DataViewForm,view,request.getSession().getAttribute("u_id").toString());

         view.setButtonVisible("0");

         if(view.getPrgAction()!=null && view.getPrgAction().equals("add")){
            view.setAllClear();
            view.setTipMessage("請輸入下列欄位，星號為必填欄位。輸入完畢請按存檔");
            //flow.preAddAction(request.getSession().getAttribute("u_id").toString());
            view.setprg_no(strPrgNo);
            view.setPrgStatus("add");
            view.setInsertShow("");
            view.setButtonVisible("1");
            return "";
         }

         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("add")){
            //view.setOnloadScript("alert('" + flow.addAction() + "');");
            view.setButtonVisible("2");
            view.setprg_no_readonly(true);
            view.setInsertShow("");
            return "";
         }

         //查詢
        if(view.getPrgAction()!=null && view.getPrgAction().equals("query")){
         view.setButtonVisible("3");

         view.setTipMessage("若欲修改資料，請於下方修改後按【整批存檔】;若欲刪除資料，則在查詢資料列按刪除按鈕");
        }


         // 修改存檔
         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("query")){
            view.setOnloadScript("alert('" + business.exeUpdSQL(view) + "');");
            view.setButtonVisible("2");
            view.setprg_no_readonly(true);
            view.setPrgStatus("save");
            return "";
         }

         //按整批存檔
         if(view.getPrgAction()!=null && view.getPrgAction().equals("saveall")){
             if(view.getrowsn()==null || view.getrowsn().equals("")){
                 view.setOnloadScript("alert('沒有修改!');");
             } else{
                  view.setOnloadScript("alert('"+business.batchUpdateData(view)+"');");
             }
             view.setButtonVisible("2");
             view.setprg_no_readonly(true);
             view.setPrgStatus("save");
         }

         //按刪除
         if(view.getPrgAction()!=null && view.getPrgAction().equals("delete")){
            business.exeDelSQL(view,Integer.parseInt(view.getrowsn()));
            view.setrowsn("9999");
            return "";
         }

         // 離開
         if(view.getPrgAction()!=null && view.getPrgAction().equals("exit")){
             if (view.getPrgStatus()!=null && (view.getPrgStatus().equals("add")||view.getPrgStatus().equals("modify"))){
                 view.setAllClear();
                 view.setButtonVisible("0");
                 view.setprg_no(strPrgNo);
                 view.setSelectShow("");
                 view.setPrgStatus("exit");
                 return "";
             } else {
                view.setAllClear();
                view.setprg_no_readonly(false);
                view.setSelectShow("none");
                view.setPrgStatus("exit");
                return "";
             }
         }

         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT.');");
         return "";
      }
      finally{
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
